//调用微信云的数据
const db = wx.cloud.database()
//云数据库里面的集合collection
const musiclist = db.collection('musiclist')//数据集
const audioCtx = wx.createInnerAudioContext()//音乐播放器
Page({
    data:{
        tab:0,
        item:0,
        state:'puased',
        sjsz:[]
    },
    //当页面启动时
    onLoad(){
        //取回云数据库的数据
        //musiclist.get()是异步函数，会放在小程序的json里，等它执行完才会返回值到你指定的地址，在这期间，程序会跳过这行代码不等它执行完就接着执行下一行，让异步函数自己运行
        musiclist.get().then(res=>{
            console.log(res)
            // this.setData({musiclist:res.data,play:res.data[0]})
            this.setData({musiclist:res.data})
            this.selectMusic(0)
        })
        musiclist.get().then(res=>{
            let sjsz = []
            console.log(sjsz,res.data)
            sjsz = res.data
            var n = sjsz.length, t, i;
            while (n) {
                i = Math.random() * n-- | 0; // 0 ≤ i < n
                t = sjsz[n];
                sjsz[n] = sjsz[i];
                sjsz[i] = t;
            }
            this.setData({sjsz})
        })
        //看看是哪错了
        audioCtx.onError(err=>{
            console.log('播放失败',err,audioCtx,src)
        })
        audioCtx.onCanplay(()=>{
            //时长 duration
            this.setData({
                'play.duraion':audioCtx.duration
            })
            if(this.data.state == 'running'){
                audioCtx.play()
            }
        })
        audioCtx.onTimeUpdate(()=>{
            this.setData({
                'play.duration':audioCtx.duration,
                'play.currentTime':audioCtx.currentTime,
                'play.percent':audioCtx.currentTime / audioCtx.duration * 100
            })
        })
    },
    changeItem(e){
        this.setData({item:e.currentTarget.dataset.item})
    },
    changeTab(e){
        this.setData({tab:e.detail.current})
    },
    selectMusic(index){
        let music = this.data.musiclist[index]
        audioCtx.src = music.src
        this.setData({
            playIndex:index,
            play:music,
            'play.currentTime':0,
            'play.duration':0,
            'play.percent':0
        })
    },
    bf(e){
        let index = e.currentTarget.dataset.index
        let music_title = this.data.sjsz[index].title
        //通过两个数组的标题来获取播放列表数组里的下标
        let musiclist_index = this.data.musiclist.findIndex(function(item){
            if(item.title == music_title)return item
        });
        this.selectMusic(musiclist_index)
        this.play()
    },
    //点击
    play(){
        audioCtx.play()
        this.setData({
            state:'running'
        })
    },
    pause(){
        audioCtx.pause()
        this.setData({
            state:'paused'
        })
    },
    next(){
        //如果播的歌曲索引号大于播放列表的话就大于0，否则就下一首
        let index = this.data.playIndex>=this.data.musiclist.length-1 ? 0 : this.data.playIndex + 1
        this.selectMusic(index)
        //如果是启动，就播放音乐
        if(this.data.state == 'running'){
            this.play()
        }
    },
    previous(){
        let index = this.data.playIndex<=0 ? this.data.musiclist.length-1 : this.data.playIndex - 1
        this.selectMusic(index)
        //如果是启动，就播放音乐
        if(this.data.state == 'running'){
            this.play()
        }
    },
    change(e){
        //切歌
        this.selectMusic(e.currentTarget.dataset.index)
        this.play()
    },
    jdt(e){
        console.log(e.detail.value)
        console.log(audioCtx.duration)
        audioCtx.seek(e.detail.value*audioCtx.duration/100)
    }
})